<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\validate\CheckAdmin;
use think\exception\ValidateException;
class Admin extends Base
{
    protected $mod;
    protected function initialize()
    {
        $this->mod=\app\admin\model\Admin::class;
    }

    public function index()
    {
        $dataList = $this->mod::withoutField('password,salt')->with('roles')->select();
        return $this->success('', $dataList);
    }


    public function save()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            try {
                //验证
                if (isset($data['id'])) {
                    $tips = '更新';
                    $valCheck = validate(CheckAdmin::class)->scene('edit')->check($data);
                } else {
                    $tips = '增加';

                    $valCheck = validate(CheckAdmin::class)->check($data);
                }

                if ($valCheck !== true) {
                    $this->error($valCheck);
                }
                $data['last_ip'] = $this->request->ip();
                if (isset($data['id'])) {
                    if (isset($data['password'])) {
                        $data['salt'] = makeStr(8);
                        $data['password'] = md5($data['password'] . $data['salt']);
                    }
                    $admin = $this->mod::update($data);
                } else {
                    $data['salt'] = makeStr(8);
                    $data['password'] = md5($data['password'] . $data['salt']);
                    $admin = $this->mod::create($data);
                }
                if ($admin) {
                    return $this->success($tips . '成功');
                } else {
                    return $this->error($tips . '失败');
                }
            } catch (ValidateException $e) {
                return $this->error($e->getError());
            }
        } else {
            return $this->error('无效请求');
        }
    }


    //还原权限
    public function restore($id){
        $this->mod::update([
            'id'=>$id,
            'cluderules'=>'',
            'cludebtn'=>'',
            'excluderules'=>'',
            'excludebtn'=>''
        ]);
        return $this->success('权限己还原');
    }



    public function read($id)
    {
        $r=$this->mod::with('roles')->withoutField('password,salt')->find($id);
        return $this->success('ok',$r);
    }
    public function getadmin($id = 0)
    {
        if (!$id) {
            return;
        }
        $admin = $this->mod::withoutField('password,salt')->with('roles')->append(['sysrules'])->find($id);




        return $this->success('获取成功', $admin);
    }

    public function update($id)
    {
        //
    }
//个人权限:取个人角色权限 与 个人提交权限 差集
public function authupdate($id=0,$cludebtn=[],$cluderules=[])
{
    if ($this->request->isPost()) {
        $data = $this->request->param();
        $admin = $this->mod::with('roles')->find($id);
        if (!$admin) {
            return $this->error('用户不存在');
        }
        //继承的菜单
        $rolesRules=[];
        foreach($admin->roles as $v){
            if (is_array($v->rules)) {
                $rolesRules = array_merge($rolesRules, $v->rules);
            }
        }



        $rolesRules=array_unique($rolesRules);
        //继承的按钮
        $rolesBtn=[];
        foreach($admin->roles as $k=>$v){
            foreach($v->modbtn as $key=>$vo){
                if(isset($rolesBtn[$key])){//如果己有数据进行合并然后去重
                    $rolesBtn[$key]=array_merge($rolesBtn[$key],$vo);
                    $rolesBtn[$key]=array_unique($rolesBtn[$key]);
                }else{
                    $rolesBtn[$key]=$vo;
                }
            }
        }

        //创建个人权限更新数据
        $update['id']=$id;
        $update['cluderules']=[];
        $update['excluderules']=[];
        $update['cludebtn']=[];
        $update['excludebtn']=[];

        //筛选额外菜单
        foreach($cluderules as $v){
            if(!in_array($v,$rolesRules)){
                $update['cluderules'][]=$v;
            }
        }
        //筛选排除菜单
        foreach($rolesRules as $v){
            if(!in_array($v,$cluderules)){
                $update['excluderules'][]=$v;
            }
        }
        //筛选额外按钮
        foreach($cludebtn as $m=>$v){
           if(isset($rolesBtn[$m])){
                foreach($v as $vo){
                    if(!in_array($vo,$rolesBtn[$m])){
                        $update['cludebtn'][$m][]=$vo;
                    }
                }
           }else{
                $update['cludebtn'][$m]=$v;
           }
        }
        //筛选排除按钮
        foreach($rolesBtn as $m=>$v){
            if(isset($cludebtn[$m])){
                foreach($v as $vo){
                    if(!in_array($vo,$cludebtn[$m])){
                        $update['excludebtn'][$m][]=$vo;
                    }
                }
            }else{
                $update['excludebtn'][$m]=$v;
            }
        }
        //更新
        if ($this->mod::update($update)) {
            return $this->success('权限分配成功');
        } else {
            return $this->error('权限分配失败');
        };
    }

}

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if (!$id || $id == 1) {
            return;
        }
        if ($this->mod::destroy($id)) {
            return $this->success('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }



}
